<?php

/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
 */

class bm_best_sellers {

    var $code = 'bm_best_sellers';
    var $group = 'boxes';
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;

    function bm_best_sellers() {
        $this->title = MODULE_BOXES_BEST_SELLERS_TITLE;
        $this->description = MODULE_BOXES_BEST_SELLERS_DESCRIPTION;

        if (defined('MODULE_BOXES_BEST_SELLERS_STATUS')) {
            $this->sort_order = MODULE_BOXES_BEST_SELLERS_SORT_ORDER;
            $this->enabled = (MODULE_BOXES_BEST_SELLERS_STATUS == 'True');

            $this->group = ((MODULE_BOXES_BEST_SELLERS_CONTENT_PLACEMENT == 'Left Column') ? 'boxes_column_left' : 'boxes_column_right');
        }
    }

    function execute() {
        global $HTTP_GET_VARS, $current_category_id, $languages_id, $oscTemplate;

        if(isset($HTTP_GET_VARS['products_id'])) {
            if (isset($current_category_id) && ($current_category_id > 0)) {
                
                $best_sellers_query = tep_db_query("SELECT DISTINCT p.products_id,p.products_price,p.products_tax_class_id,p.products_model,p.products_image,pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where p.products_status = '1' and p.products_ordered > 0 and p.products_id = pd.products_id and pd.language_id = '" . (int) $languages_id . "' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and '" . (int) $current_category_id . "' in (c.categories_id, c.parent_id) order by p.products_ordered desc, pd.products_name limit " . MAX_DISPLAY_BESTSELLERS);
            } else {
                $best_sellers_query = tep_db_query("select distinct p.products_id,p.products_price,p.products_tax_class_id,p.products_model,p.products_image,pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_ordered > 0 and p.products_id = pd.products_id and pd.language_id = '" . (int) $languages_id . "' order by p.products_ordered desc, pd.products_name limit " . MAX_DISPLAY_BESTSELLERS);
            }

            if (tep_db_num_rows($best_sellers_query) >= MIN_DISPLAY_BESTSELLERS) {
                $currencies = new currencies();
                $bestsellers_list = '<ul id="mycarousel" class="jcarousel-skin-tango">';
                while ($best_sellers = tep_db_fetch_array($best_sellers_query)) 
                {
                    
                    if ($new_price = tep_get_products_special_price($best_sellers['products_id'])) 
                    {
                        //CGDiscountSpecials start
                        //$products_price = '<s>' . $currencies->display_price($best_sellers['products_price'], tep_get_tax_rate($best_sellers['products_tax_class_id'])) . '</s><br/><span class="productSpecialPrice">' . $currencies->display_price_nodiscount($new_price, tep_get_tax_rate($product_info['products_tax_class_id'])) . '</span>';
                        
                        $products_price = '<span class="productSpecialPrice">' . $currencies->display_price_nodiscount($new_price, tep_get_tax_rate($product_info['products_tax_class_id'])) . '</span>';
                        //CGDiscountSpecials end
                    }
                    else
                    {
                        $products_price = $currencies->display_price($best_sellers['products_price'], tep_get_tax_rate($best_sellers['products_tax_class_id']));
                    }
                    
                    $bestsellers_list .= '<li>
                                            <div class="scroll-product">
                                                    <a href="'.tep_href_link(FILENAME_PRODUCT_INFO, 'products_id='.$best_sellers['products_id']) . '">'.tep_image(DIR_WS_IMAGES . $best_sellers['products_image'], $best_sellers['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT).'</a>
                                                    <div class="spacer15"></div>
                                                    <div class="p-title">
                                                        <a href="'.tep_href_link(FILENAME_PRODUCT_INFO, 'products_id='.$best_sellers['products_id']).'">'.$best_sellers['products_name'].'</a>
                                                    </div>
                                                    <div class="spacer"></div>
                                                    <div class="p-subtitle">'.$best_sellers['products_model'].'</div>
                                                    <div class="spacer"></div>
                                                    <div class="p-price">'.$products_price.'</div>
                                                </div>
                                            <div class="spacer50"></div></li>';
                }
                $bestsellers_list .= '</ul>';

                $data = '<div class="right-part">
                                <div class="other-product"></div>
                                <div class="scroll-product-bg">
                                    <div class="scroll-mid-bg ">'
                                     . $bestsellers_list .
                                    '</div>
                                 </div>
                          </div>';

                //$oscTemplate->addBlock($data, $this->group);
                return $data;
            }
        }
    }

    function isEnabled() {
        return $this->enabled;
    }

    function check() {
        return defined('MODULE_BOXES_BEST_SELLERS_STATUS');
    }

    function install() {
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Best Sellers Module', 'MODULE_BOXES_BEST_SELLERS_STATUS', 'True', 'Do you want to add the module to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Placement', 'MODULE_BOXES_BEST_SELLERS_CONTENT_PLACEMENT', 'Right Column', 'Should the module be loaded in the left or right column?', '6', '1', 'tep_cfg_select_option(array(\'Left Column\', \'Right Column\'), ', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_BOXES_BEST_SELLERS_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
    }

    function remove() {
        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
        return array('MODULE_BOXES_BEST_SELLERS_STATUS', 'MODULE_BOXES_BEST_SELLERS_CONTENT_PLACEMENT', 'MODULE_BOXES_BEST_SELLERS_SORT_ORDER');
    }
}
?>